<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>接口幂等性设计指南</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 8px;
            padding: 1.5rem;
            overflow-x: auto;
            font-family: 'Consolas', 'Monaco', monospace;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        }
        .sql-keyword {
            color: #569cd6;
            font-weight: bold;
        }
        .sql-string {
            color: #ce9178;
        }
        .sql-number {
            color: #b5cea8;
        }
        .section-divider {
            height: 2px;
            background: linear-gradient(to right, transparent, #e0e0e0, transparent);
            margin: 3rem 0;
        }
        .floating-icon {
            animation: float 3s ease-in-out infinite;
        }
        @keyframes float {
            0%, 100% { transform: translateY(0px); }
            50% { transform: translateY(-10px); }
        }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.5rem;
            font-weight: 700;
            color: #667eea;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center">
            <div class="floating-icon inline-block mb-6">
                <i class="fas fa-shield-alt text-6xl opacity-80"></i>
            </div>
            <h1 class="text-5xl md:text-6xl font-bold mb-6" style="font-family: 'Noto Serif SC', serif;">
                接口幂等性设计指南
            </h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                确保系统操作的一致性与可靠性，让每一次请求都产生预期的结果
            </p>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        
        <!-- 背景介绍 -->
        <section class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <div class="flex items-center mb-6">
                <i class="fas fa-lightbulb text-3xl text-yellow-500 mr-4"></i>
                <h2 class="text-3xl font-bold text-gray-800">为什么需要幂等性？</h2>
            </div>
            <p class="text-lg text-gray-700 leading-relaxed mb-6">
                <span class="drop-cap">在</span>分布式系统中，网络延迟、系统故障或用户误操作都可能导致请求重复发送。如果不考虑幂等性，这些重复请求可能会产生意想不到的副作用。
            </p>
            <div class="grid md:grid-cols-2 gap-6 mt-8">
                <div class="bg-gradient-to-br from-blue-50 to-indigo-50 p-6 rounded-xl">
                    <i class="fas fa-mouse text-2xl text-blue-600 mb-3"></i>
                    <h3 class="text-xl font-semibold mb-2">前端重复提交</h3>
                    <p class="text-gray-700">用户多次点击提交按钮，后台应只处理一次</p>
                </div>
                <div class="bg-gradient-to-br from-green-50 to-emerald-50 p-6 rounded-xl">
                    <i class="fas fa-credit-card text-2xl text-green-600 mb-3"></i>
                    <h3 class="text-xl font-semibold mb-2">支付请求</h3>
                    <p class="text-gray-700">网络重发不应导致重复扣款</p>
                </div>
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 p-6 rounded-xl">
                    <i class="fas fa-envelope text-2xl text-purple-600 mb-3"></i>
                    <h3 class="text-xl font-semibold mb-2">消息发送</h3>
                    <p class="text-gray-700">同一条短信只应发送一次</p>
                </div>
                <div class="bg-gradient-to-br from-orange-50 to-red-50 p-6 rounded-xl">
                    <i class="fas fa-shopping-cart text-2xl text-orange-600 mb-3"></i>
                    <h3 class="text-xl font-semibold mb-2">订单创建</h3>
                    <p class="text-gray-700">一次业务请求只能创建一个订单</p>
                </div>
            </div>
        </section>

        <div class="section-divider"></div>

        <!-- 幂等性概念 -->
        <section class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <div class="flex items-center mb-6">
                <i class="fas fa-book text-3xl text-indigo-500 mr-4"></i>
                <h2 class="text-3xl font-bold text-gray-800">什么是幂等性？</h2>
            </div>
            <div class="bg-gradient-to-r from-indigo-100 to-purple-100 p-6 rounded-xl">
                <p class="text-lg text-gray-800 leading-relaxed">
                    <strong>幂等性（Idempotence）</strong>是指用户对于同一操作发起的一次请求或者多次请求的结果是一致的，不会因为多次点击而产生副作用。
                </p>
            </div>
            <div class="mt-8">
                <div class="mermaid">
                    graph LR
                        A[用户请求] --> B{是否幂等?}
                        B -->|是| C[执行一次]
                        B -->|是| D[执行N次]
                        C --> E[相同结果]
                        D --> E
                        B -->|否| F[执行一次]
                        B -->|否| G[执行N次]
                        F --> H[结果1]
                        G --> I[结果N]
                        style E fill:#90EE90
                        style H fill:#FFB6C1
                        style I fill:#FFB6C1
                </div>
            </div>
        </section>

        <div class="section-divider"></div>

        <!-- 操作类型分析 -->
        <section class="bg-white rounded-2xl shadow-xl p-8 mb-8">
            <div class="flex items-center mb-6">
                <i class="fas fa-code text-3xl text-green-500 mr-4"></i>
                <h2 class="text-3xl font-bold text-gray-800">不同操作的幂等性分析</h2>
            </div>

            <!-- 添加操作 -->
            <div class="mb-10">
                <h3 class="text-2xl font-semibold mb-4 flex items-center">
                    <i class="fas fa-plus-circle text-blue-500 mr-3"></i>
                    添加操作
                </h3>
                <div class="grid md:grid-cols-2 gap-6">
                    <div class="border-2 border-red-200 rounded-xl p-6 bg-red-50">
                        <h4 class="text-lg font-semibold mb-3 text-red-700">
                            <i class="fas fa-times-circle mr-2"></i>自增主键 - 无幂等性
                        </h4>
                        <div class="code-block">
                            <span class="sql-keyword">INSERT INTO</span> product(name, type, price, tm)
                            <br><span class="sql-keyword">VALUES</span> (<span class="sql-string">'iPhone'</span>, <span class="sql-string">'手机'</span>, <span class="sql-number">5999</span>, now())
                        </div>
                        <p class="mt-3 text-gray-700">执行多次会插入多条记录</p>
                    </div>
                    <div class="border-2 border-green-200 rounded-xl p-6 bg-green-50">
                        <h4 class="text-lg font-semibold mb-3 text-green-700">
                            <i class="fas fa-check-circle mr-2"></i>业务主键 - 有幂等性
                        </h4>
                        <div class="code-block">
                            <span class="sql-keyword">INSERT INTO</span> product(orderId, name, type, price, tm)
                            <br><span class="sql-keyword">VALUES</span> (<span class="sql-string">'ORD001'</span>, <span class="sql-string">'iPhone'</span>, <span class="sql-string">'手机'</span>, <span class="sql-number">5999</span>, now())
                        </div>
                        <p class="mt-3 text-gray-700">利用主键唯一性，只会插入一条</p>
                    </div>
                </div>
            </div>

            <!-- 删除操作 -->
            <div class="mb-10">
                <h3 class="text-2xl font-semibold mb-4 flex items-center">
                    <i class="fas fa-trash-alt text-red-500 mr-3"></i>
                    删除操作
                </h3>
                <div class="grid md:grid-cols-2 gap-6">
                    <div class="border-2 border-green-200 rounded-xl p-6 bg-green-50">
                        <h4 class="text-lg font-semibold mb-3 text-green-700">
                            <i class="fas fa-check-circle mr-2"></i>主键删除 - 有幂等性
                        </h4>
                        <div class="code-block">
                            <span class="sql-keyword">DELETE FROM</span> product 
                            <br><span class="sql-keyword">WHERE</span> id = <span class="sql-number">3</span>
                        </div>
                        <p class="mt-3 text-gray-700">无论执行多少次，只删除一条</p>
                    </div>
                    <div class="border-2 border-red-200 rounded-xl p-6 bg-red-50">
                        <h4 class="text-lg font-semibold mb-3 text-red-700">
                            <i class="fas fa-times-circle mr-2"></i>条件删除 - 无幂等性
                        </h4>
                        <div class="code-block">
                            <span class="sql-keyword">DELETE FROM</span> product 
                            <br><span class="sql-keyword">WHERE</span> id > <span class="sql-number">23</span>
                        </div>
                        <p class="mt-3 text-gray-700">每次执行可能删除不同数量的记录</p>
                    </div>
                </div>
            </div>

            <!-- 修改操作 -->
            <div class="mb-10">
                <h3 class="text-2xl font-semibold mb-4 flex items-center">
                    <i class="fas fa-edit text-orange-500 mr-3"></i>
                    修改操作
                </h3>
                <div class="grid md:grid-cols-2 gap-6">
                    <div class="border-2 border-green-200 rounded-xl p-6 bg-green-50">
                        <h4 class="text-lg font-semibold mb-3 text-green-700">
                            <i class="fas fa-check-circle mr-2"></i>直接赋值 - 有幂等性
                        </h4>
                        <div class="code-block">
                            <span class="sql-keyword">UPDATE</span> goods 
                            <br><span class="sql-keyword">SET</span> stock = <span class="sql-number">586</span> 
                            <br><span class="sql-keyword">WHERE</span> id = <span class="sql-number">10</span>
                        </div>
                        <p class="mt-3 text-gray-700">无论执行多少次，结果相同</p>
                    </div>
                    <div class="border-2 border-red-200 rounded-xl p-6 bg-red-50">
                        <h4 class="text-lg font-semibold mb-3 text-red-700">
                            <i class="fas fa-times-circle mr-2"></i>累加操作 - 无幂等性
                        </h4>
                        <div class="code-block">
                            <span class="sql-keyword">UPDATE</span> goods 
                            <br><span class="sql-keywor